

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cheney">
  <meta name="keywords" content="Coding">
  
    <meta name="description" content="5个不同国家且工作各不相同的人分别住在一条街上的5所房子里，每所房子的颜色不同，每个人都有自己养的不同宠物，喜欢喝不同的饮料。根据以下提示，你能告诉我哪所房子里的人养斑马，哪所房子里的人喜欢喝矿泉水吗？">
<meta property="og:type" content="article">
<meta property="og:title" content="斑马问题多方法求解">
<meta property="og:url" content="https://cheney822.gitee.io/2021/10/01/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD__%E6%96%91%E9%A9%AC%E9%97%AE%E9%A2%98%E5%A4%9A%E6%96%B9%E6%B3%95%E6%B1%82%E8%A7%A3/index.html">
<meta property="og:site_name" content="Cheney blog">
<meta property="og:description" content="5个不同国家且工作各不相同的人分别住在一条街上的5所房子里，每所房子的颜色不同，每个人都有自己养的不同宠物，喜欢喝不同的饮料。根据以下提示，你能告诉我哪所房子里的人养斑马，哪所房子里的人喜欢喝矿泉水吗？">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/7f44a7896bae4caf82acc6db03e10572.png">
<meta property="og:image" content="https://gitee.com/Cheney822/images/raw/master/PicGo/e55bf0fda3774b22979dd440d920ab12.png">
<meta property="article:published_time" content="2021-10-01T05:14:00.000Z">
<meta property="article:modified_time" content="2022-04-05T13:14:31.395Z">
<meta property="article:author" content="Cheney">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="逻辑推理">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://imgbed.cheney.cc/picgo/7f44a7896bae4caf82acc6db03e10572.png">
  
  
  <title>斑马问题多方法求解 - Cheney blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"cheney822.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":1},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Cheney Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://imgbed.cheney.cc/Blog_config/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="斑马问题多方法求解">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-10-01 13:14" pubdate>
        2021年10月1日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      9.2k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      46 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">斑马问题多方法求解</h1>
            
            <div class="markdown-body">
              <p><kbd> ==人工智能导论实验导航== </kbd></p>
<p>==实验一：斑马问题== <a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_46291251/article/details/122246347">https://blog.csdn.net/weixin_46291251/article/details/122246347</a></p>
<p>==实验二：图像恢复== <a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_46291251/article/details/122561220">https://blog.csdn.net/weixin_46291251/article/details/122561220</a></p>
<p>==实验三：花卉识别== <a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_46291251/article/details/122561505">https://blog.csdn.net/weixin_46291251/article/details/122561505</a></p>
<p>==实验四：手写体生成== <a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_46291251/article/details/122576478">https://blog.csdn.net/weixin_46291251/article/details/122576478</a></p>
<p>==实验源码：== xxx</p>
<h1 id="1-1实验介绍"><a href="#1-1实验介绍" class="headerlink" title="1.1实验介绍"></a>1.1实验介绍</h1><h2 id="1-1-1实验背景"><a href="#1-1-1实验背景" class="headerlink" title="1.1.1实验背景"></a>1.1.1实验背景</h2><p>演绎推理（Deductive Reasoning）是由一般到特殊的推理方法。与“归纳法”相对。推论前提与结论之间的联系是必然的，是一种确实性推理。<br>运用此法研究问题，首先要正确掌握作为指导思想或依据的一般原理、原则；其次要全面了解所要研究的课题、问题的实际情况和特殊性；然后才能推导出一般原理用于特定事物的结论。<br>演绎推理的形式有三段论、假言推理和选言推理等。在教育工作中， 依据一定的科学原理设计和进行教育与教学实验等，均离不开此法。</p>
<h2 id="1-1-2实验目的"><a href="#1-1-2实验目的" class="headerlink" title="1.1.2实验目的"></a>1.1.2实验目的</h2><p>本章实验的主要目的是掌握逻辑与推理相关基础知识点，了解算法推理相关基础知识，掌握归纳法和演绎推理的主要步骤，熟悉python编程。</p>
<p>通过实验，基本掌握逻辑编程的思想，了解逻辑编程与命令式编程的区别。能够依据给定的事实以及规则编写代码，解决逻辑约束问题(CLP)。</p>
<h2 id="1-1-3实验简介"><a href="#1-1-3实验简介" class="headerlink" title="1.1.3实验简介"></a>1.1.3实验简介</h2><p>5个不同国家且工作各不相同的人分别住在一条街上的5所房子里，每所房子的颜色不同，每个人都有自己养的不同宠物，喜欢喝不同的饮料。<br>根据以下提示，你能告诉我哪所房子里的人养斑马，哪所房子里的人喜欢喝矿泉水吗？</p>
<h3 id="存在的条件汇总："><a href="#存在的条件汇总：" class="headerlink" title="存在的条件汇总："></a>存在的条件汇总：</h3><ul>
<li>国籍：英国、西班牙、日本、意大利、挪威</li>
<li>房子颜色：红色、绿色、白色、蓝色、黄色</li>
<li>工作：油漆工、摄影师、外交官、小提琴家、医师</li>
<li>宠物：狗、蜗牛、狐狸、马、斑马</li>
<li>饮料：茶、牛奶、咖啡、橘子汁、矿泉水</li>
</ul>
<h3 id="已知条件如下："><a href="#已知条件如下：" class="headerlink" title="已知条件如下："></a>已知条件如下：</h3><ul>
<li><p>英国人住在红色的房子里</p>
</li>
<li><p>西班牙人养了一条狗</p>
</li>
<li><p>日本人是一个油漆工</p>
</li>
<li><p>意大利人喜欢喝茶</p>
</li>
<li><p>挪威人住在左边的第一个房子里</p>
</li>
<li><p>绿房子在白房子的右边</p>
</li>
<li><p>摄影师养了一只蜗牛</p>
</li>
<li><p>外交官住在黄房子里</p>
</li>
<li><p>中间那个房子的人喜欢喝牛奶</p>
</li>
<li><p>喜欢喝咖啡的人住在绿房子里</p>
</li>
<li><p>挪威人住在蓝色的房子旁边</p>
</li>
<li><p>小提琴家喜欢喝橘子汁</p>
</li>
<li><p>养狐狸的人所住的房子与医生的房子相邻</p>
</li>
<li><p>养马的人所住的房子与外交官的房子相邻</p>
<h1 id="1-2概要设计"><a href="#1-2概要设计" class="headerlink" title="1.2概要设计"></a>1.2概要设计</h1><h2 id="1-2-1手动求解"><a href="#1-2-1手动求解" class="headerlink" title="1.2.1手动求解"></a>1.2.1手动求解</h2></li>
<li><p>1.罗列初始条件。</p>
</li>
<li><p>2.首先由“英国人住在红色的房子里，绿房子在白房子的右边，外交官住在黄房子里”以及上述条件可知：<br>a：红白绿房子必在右边三个房子。<br>  b：挪威人必住在黄房子且他是外交官<br>c：红房子要么是第三间，要么是第五间。</p>
</li>
<li><p>3.然后假设在红房子在第三间外交官，可得：<br>  a.由表格知挪威人不喝咖啡和牛奶，又意大利喝茶，小提琴家喝桔子汁（挪威人是外交官），所以挪威人喝矿泉水。<br>  b.由小提琴家和桔子汁知，小提琴家不是英国人（喝牛奶），挪威人（矿泉水），意大利人（茶），日本人（油漆工），所以小提琴家是西班牙人。</p>
</li>
<li><p>4.结合上述分析可得<br>a.由“养狐狸的人所住的房子与医师的房子相邻”知，医师不可能在绿色和红色房子（相邻宠物不为狐狸），也不可能在黄色房子（外交官），所以医师是蓝色房子。<br>  b.由外交官，小提琴，油漆工的国籍已定，且医师不是英国人（蓝色房子），知摄影师是英国人，养了蜗牛。所以挪威人养了狐狸（养狐狸的人所住的房子与医师的房子相邻）。<br>  c.狐狸，马，蜗牛，狗的主人身份已确定，知斑马是油漆工，即日本人的宠物。<br>d.由矿泉水，牛奶，桔子汁，咖啡主人已确定，且意大利人喝茶，知意大利人所住房子为蓝色。</p>
</li>
<li><p>5.综合以上分析可得下面的表格：<br><img src="https://imgbed.cheney.cc/picgo/7f44a7896bae4caf82acc6db03e10572.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
</li>
</ul>
<h2 id="1-2-2-python求解"><a href="#1-2-2-python求解" class="headerlink" title="1.2.2 python求解"></a>1.2.2 python求解</h2><p>通过上述分析，结合python求解逻辑推理问题的常见库，得到以下思路：</p>
<h3 id="解法1：穷举法"><a href="#解法1：穷举法" class="headerlink" title="解法1：穷举法"></a>解法1：穷举法</h3><p>将所有可能的组合通过全排列的形式穷举出来<br>然后根据已有的条件依次匹配<br>若全部通过则为正确答案<br>否则为错误方案</p>
<h3 id="解法2："><a href="#解法2：" class="headerlink" title="解法2："></a>解法2：</h3><p>利用google or-tools 。<br>OR-Tools是一个用于优化的开源软件套件，用于解决车辆路径、流程、整数和线性规划以及约束编程等世界上最棘手的问题。<br>当前ortools提供的优化器包括： - 约束规划 - 线性与混合整数规划 - 路径规划 - 调度规划 - 网络规划 - 装箱。<br>并且在其GitHub的源代码中有解决斑马问题的示例代码，地址如下：<br><a target="_blank" rel="noopener" href="https://github.com/google/or-tools/blob/master/examples/python/zebra_sat.py">https://github.com/google/or-tools/blob/master/examples/python/zebra_sat.py</a></p>
<h3 id="解法3："><a href="#解法3：" class="headerlink" title="解法3："></a>解法3：</h3><p>使用python逻辑编程kanren库，将题目中的条件形式化为一个个的表达式，加入约束到kanren的一个集合中，然后利用kanren内置的run求解即可。</p>
<h3 id="综上："><a href="#综上：" class="headerlink" title="综上："></a>综上：</h3><p>通过上述对三种方法的分析可知，方法1过于简单粗暴，能够简单的解决问题，但是算法的复杂度太高，不可取。<br>方法二和方法三都是通过调用python对应的逻辑分析库完成，但鉴于方法2的google or-tools 官方已有较为完善的示例代码，与独立完成实验的要求不符，故采用方法3。下面只给出方法3的解法设计与分析。</p>
<h1 id="1-3详细设计"><a href="#1-3详细设计" class="headerlink" title="1.3详细设计"></a>1.3详细设计</h1><h2 id="首先导入实验所需的包"><a href="#首先导入实验所需的包" class="headerlink" title="首先导入实验所需的包"></a>首先导入实验所需的包</h2><figure class="highlight python"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">from</span> kanren <span class="hljs-keyword">import</span> *<br><span class="hljs-keyword">from</span> kanren.core <span class="hljs-keyword">import</span> lall<br></code></pre></td></tr></table></figure>

<p>下面根据已知条件的性质将条件分为几组，首先有着确定关系的实体和其属性的条件，以及同属于一个实体的属性间的关系，有如下几个：</p>
<ul>
<li>英国人住在红房子里</li>
<li>西班牙人养了一条狗</li>
<li>日本人是一个油漆工</li>
<li>意大利人喝茶。</li>
<li>摄影师养了一只蜗牛</li>
<li>外交官住在黄房子里</li>
<li>外交官住在黄房子里</li>
<li>喜欢喝咖啡的人住在绿房子里</li>
<li>小提琴家喜欢喝橘子汁</li>
</ul>
<h2 id="然后介绍kanren中加入一个逻辑表达式的方法："><a href="#然后介绍kanren中加入一个逻辑表达式的方法：" class="headerlink" title="然后介绍kanren中加入一个逻辑表达式的方法："></a>然后介绍kanren中加入一个逻辑表达式的方法：</h2><p>首先用houses = var() 创建一个逻辑变量house。<br>然后利用membero函数表示一个约束, membero(item, coll) 表示 item 是 coll集合中的一个成员。如：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python">(membero, (<span class="hljs-string">&#x27;Britain&#x27;</span>, var(), var(), var(), <span class="hljs-string">&#x27;red&#x27;</span>), houses),  <span class="hljs-comment"># 英国人住在红房子里</span><br></code></pre></td></tr></table></figure>

<p> 代码中的var表示任意变量。<br>5个var（）分别代表 国籍、工作、饮料、宠物、屋子颜色 </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python">(membero, (<span class="hljs-string">&#x27;Britain&#x27;</span>, var(), var(), var(), <span class="hljs-string">&#x27;red&#x27;</span>), houses),  <span class="hljs-comment"># 英国人住在红房子里</span><br><br>(membero, (<span class="hljs-string">&#x27;Spain&#x27;</span>, var(), var(), <span class="hljs-string">&#x27;dog&#x27;</span>, var()), houses),  <span class="hljs-comment"># 西班牙人养了一条狗</span><br><br>(membero, (<span class="hljs-string">&#x27;Japan&#x27;</span>, <span class="hljs-string">&#x27;painter&#x27;</span>, var(), var(), var()), houses),  <span class="hljs-comment"># 日本人是一个油漆工</span><br><br>(membero, (<span class="hljs-string">&#x27;Italy&#x27;</span>, var(), <span class="hljs-string">&#x27;tea&#x27;</span>, var(), var()), houses),  <span class="hljs-comment"># 意大利人喝茶。</span><br><br>(membero, (var(), <span class="hljs-string">&#x27;Photographer&#x27;</span>, var(), <span class="hljs-string">&#x27;snail&#x27;</span>, var()), houses),  <span class="hljs-comment"># 摄影师养了一只蜗牛</span><br><br>(membero, (var(), <span class="hljs-string">&#x27;diplomat&#x27;</span>, var(), var(), <span class="hljs-string">&#x27;yellow&#x27;</span>), houses),  <span class="hljs-comment"># 外交官住在黄房子里</span><br><br>(membero, (var(), var(), <span class="hljs-string">&#x27;coffee&#x27;</span>, var(), <span class="hljs-string">&#x27;green&#x27;</span>), houses),  <span class="hljs-comment"># 喜欢喝咖啡的人住在绿房子里</span><br><br>(membero, (var(), <span class="hljs-string">&#x27;Violinist&#x27;</span>, <span class="hljs-string">&#x27;juice&#x27;</span>, var(), var()), houses),  <span class="hljs-comment"># 小提琴家喜欢喝橘子汁</span><br></code></pre></td></tr></table></figure>


<p>然后是表示实体在整体中的确切属性的条件，有以下两个：</p>
<ul>
<li>挪威人住在左边的第一个房子里</li>
<li>中间房子的人喜欢喝牛奶</li>
</ul>
<p>添加方法和第一种一样，不一样的是这里用的是eq函数，eq(a,b ) 表示ab相等。  </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python">(eq, ((<span class="hljs-string">&#x27;Norwegian&#x27;</span>, var(), var(), var(), var())<br>      , var(), var(), var(), var()), houses),  <span class="hljs-comment"># 挪威人住在左边的第一个房子里</span><br><br>(eq, (var(), var(),<br>      (var(), var(), <span class="hljs-string">&#x27;milk&#x27;</span>, var(), var()),<br>      var(), var()), houses),  <span class="hljs-comment"># 中间那个房子的人喜欢喝牛奶</span><br></code></pre></td></tr></table></figure>

<p>然后是描述两个实体间的不确定的(模糊的)关系的表达式，有以下几个：</p>
<ul>
<li>绿房子在白房子的右边</li>
<li>挪威人住在蓝房子旁边。</li>
<li>养狐狸的人所住的房子与医生的房子相邻</li>
<li>养马的人所住的房子与外交官的房子相邻</li>
</ul>
<p>但是在kanren中没有能够直接描述以上关系的函数，所以定义了以下两个函数，用来实现上述要求：<br>其中利用到的kanren内的函数：</p>
<ul>
<li>zip() 函数用于将可迭代的对象作为参数，将对象中对应的元素打包成一个个元组，然后返回由这些元组组成的列表。 </li>
<li>CONDE：一个用于逻辑和/或OR的目标构造函数。</li>
</ul>
<p>实现的函数：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs python">Left(a,b，x)表示在关系集合x中a在b的右侧<br>Next(a,b，x)表示在关系集合x中a和b相邻<br></code></pre></td></tr></table></figure>

<p>其中next是借助left表示出来的，即：两个变量a,b相邻只有两种情况，a在b左边和b在a左边。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">left</span>(<span class="hljs-params">q, p, <span class="hljs-built_in">list</span></span>):<br>    <span class="hljs-keyword">return</span> membero((q, p), <span class="hljs-built_in">zip</span>(<span class="hljs-built_in">list</span>, <span class="hljs-built_in">list</span>[<span class="hljs-number">1</span>:]))<br><br><br><span class="hljs-keyword">def</span> <span class="hljs-title function_">next</span>(<span class="hljs-params">q, p, <span class="hljs-built_in">list</span></span>):  <span class="hljs-comment"># p q 相邻意味着要么p在q左边，要么q在p左边</span><br>    <span class="hljs-keyword">return</span> conde([left(q, p, <span class="hljs-built_in">list</span>)], [left(p, q, <span class="hljs-built_in">list</span>)])<br></code></pre></td></tr></table></figure>

<p>然后借助以上两个函数就可以将对应的四个逻辑表达式表达出来了：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs python">(left,  <span class="hljs-comment"># 绿房子在白房子的右边</span><br> (var(), var(), var(), var(), <span class="hljs-string">&#x27;green&#x27;</span>),<br> (var(), var(), var(), var(), <span class="hljs-string">&#x27;white&#x27;</span>),<br> houses),<br><br>(<span class="hljs-built_in">next</span>, (<span class="hljs-string">&#x27;Norwegian&#x27;</span>, var(), var(), var(), var()),<br> (var(), var(), var(), var(), <span class="hljs-string">&#x27;blue&#x27;</span>), houses),  <span class="hljs-comment"># 挪威人住在蓝房子旁边。</span><br><br>(<span class="hljs-built_in">next</span>, (var(), <span class="hljs-string">&#x27;physician&#x27;</span>, var(), var(), var()),  <span class="hljs-comment"># 养狐狸的人所住的房子与医生的房子相邻</span><br> (var(), var(), var(), <span class="hljs-string">&#x27;fox&#x27;</span>, var()), houses),<br><br>(<span class="hljs-built_in">next</span>, (var(), <span class="hljs-string">&#x27;diplomat&#x27;</span>, var(), var(), var()),  <span class="hljs-comment"># 养马的人所住的房子与外交官的房子相邻</span><br> (var(), var(), var(), <span class="hljs-string">&#x27;horse&#x27;</span>, var()), houses),<br></code></pre></td></tr></table></figure>

<p>到这里所有的表达式都已经表达完毕，已经可以利用run运行了，但是此时的表达式中还没有斑马和矿泉水这两个变量，程序处理到他们时会用符号代替，下面添加这两个变量进入表达式：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python">(membero, (var(), var(), var(), <span class="hljs-string">&#x27;zebra&#x27;</span>, var()), houses),  <span class="hljs-comment"># 有人养斑马</span><br><br>(membero, (var(), var(), <span class="hljs-string">&#x27;water&#x27;</span>, var(), var()), houses)  <span class="hljs-comment"># 有人喝水</span><br></code></pre></td></tr></table></figure>


<p>现在完成了所有表达式和变量的表达。<br>接下来开始运算。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python">solutions = run(<span class="hljs-number">0</span>, houses, rules_zebraproblem) <br></code></pre></td></tr></table></figure>

<p>根据运算的结果，判断谁是斑马的主人以及谁爱喝矿泉水。</p>
<h1 id="判断是否有解"><a href="#判断是否有解" class="headerlink" title="判断是否有解"></a>判断是否有解</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(solutions):<br>    zebra_owner = <span class="hljs-string">&quot;&quot;</span><br>    water_drinker = <span class="hljs-string">&quot;&quot;</span><br>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> solutions[<span class="hljs-number">0</span>]:<br>        <span class="hljs-keyword">if</span> <span class="hljs-string">&quot;zebra&quot;</span> <span class="hljs-keyword">in</span> i:<br>            zebra_owner = i[<span class="hljs-number">0</span>]  <span class="hljs-comment"># 找到斑马的主人</span><br>        <span class="hljs-keyword">if</span> <span class="hljs-string">&quot;water&quot;</span> <span class="hljs-keyword">in</span> i:<br>            water_drinker = i[<span class="hljs-number">0</span>]  <span class="hljs-comment"># 找到喝矿泉水的人</span><br>        <span class="hljs-built_in">print</span>(i)<br><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;\nzebra_owner:\t\t&#x27;</span> + zebra_owner)  <span class="hljs-comment"># 打印结果</span><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;water_drinker:\t&#x27;</span> + water_drinker)<br><br><span class="hljs-keyword">else</span>:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;no answer&quot;</span>)<br></code></pre></td></tr></table></figure>

<p>程序设计完毕，下面进行运行测试。</p>
<h1 id="1-4运行测试"><a href="#1-4运行测试" class="headerlink" title="1.4运行测试"></a>1.4运行测试</h1><p>运行程序，输出结果<br><img src="https://gitee.com/Cheney822/images/raw/master/PicGo/e55bf0fda3774b22979dd440d920ab12.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>分析程序输出的结果可知，日本人的宠物是斑马，挪威人爱喝矿泉水。<br>结合前面手动计算出的结果，可知两个结果完全一致，程序正确运行。</p>
<h1 id="穷举法"><a href="#穷举法" class="headerlink" title="穷举法"></a>穷举法</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># -*- coding: GBK -*-</span><br><span class="hljs-keyword">import</span> time<br><span class="hljs-keyword">import</span> itertools<br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">本题的思路：</span><br><span class="hljs-string">    将所有可能的组合通过全排列的形式穷举出来</span><br><span class="hljs-string">    然后根据已有的条件依次匹配</span><br><span class="hljs-string">    若全部通过则为正确答案</span><br><span class="hljs-string">    否则为错误方案</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><br><br><span class="hljs-comment"># **********************匹配函数******************************</span><br><br><span class="hljs-keyword">def</span> <span class="hljs-title function_">handle</span>(<span class="hljs-params">data</span>):<br>    <span class="hljs-comment"># 英国人住红色房子</span><br>    index = data[<span class="hljs-number">0</span>].index(<span class="hljs-string">&#x27;红色&#x27;</span>)<br>    <span class="hljs-keyword">if</span> data[<span class="hljs-number">1</span>][index] != <span class="hljs-string">&#x27;英国&#x27;</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span><br><br>    <span class="hljs-comment"># 瑞典人养狗</span><br>    index = data[<span class="hljs-number">4</span>].index(<span class="hljs-string">&#x27;狗&#x27;</span>)<br>    <span class="hljs-keyword">if</span> data[<span class="hljs-number">1</span>][index] != <span class="hljs-string">&#x27;瑞典&#x27;</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">2</span><br><br>    <span class="hljs-comment"># 丹麦人喝茶</span><br>    index = data[<span class="hljs-number">2</span>].index(<span class="hljs-string">&#x27;茶&#x27;</span>)<br>    <span class="hljs-keyword">if</span> data[<span class="hljs-number">1</span>][index] != <span class="hljs-string">&#x27;丹麦&#x27;</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">3</span><br><br>    <span class="hljs-comment"># 抽Pall Mall香烟的人养鸟</span><br>    index = data[<span class="hljs-number">3</span>].index(<span class="hljs-string">&#x27;Pall Mall&#x27;</span>)<br>    <span class="hljs-keyword">if</span> data[<span class="hljs-number">4</span>][index] != <span class="hljs-string">&#x27;鸟&#x27;</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">6</span><br><br>    <span class="hljs-comment"># 黄色房子主人抽Dunhill香烟</span><br>    index = data[<span class="hljs-number">3</span>].index(<span class="hljs-string">&#x27;Dunhill&#x27;</span>)<br>    <span class="hljs-keyword">if</span> data[<span class="hljs-number">0</span>][index] != <span class="hljs-string">&#x27;黄色&#x27;</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">7</span><br><br>    <span class="hljs-comment"># 抽Blue Master的人喝啤酒</span><br>    index = data[<span class="hljs-number">3</span>].index(<span class="hljs-string">&#x27;BlueMaster&#x27;</span>)<br>    <span class="hljs-keyword">if</span> data[<span class="hljs-number">2</span>][index] != <span class="hljs-string">&#x27;啤酒&#x27;</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">12</span><br><br>    <span class="hljs-comment"># 德国人抽Prince香烟</span><br>    index = data[<span class="hljs-number">3</span>].index(<span class="hljs-string">&#x27;Prince&#x27;</span>)<br>    <span class="hljs-keyword">if</span> data[<span class="hljs-number">1</span>][index] != <span class="hljs-string">&#x27;德国&#x27;</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">13</span><br><br>    <span class="hljs-comment"># 绿色房子在白色房子左面</span><br>    index = data[<span class="hljs-number">0</span>].index(<span class="hljs-string">&#x27;绿色&#x27;</span>)<br>    <span class="hljs-keyword">if</span> index == <span class="hljs-number">4</span>:  <span class="hljs-comment"># 绿色房子在最后</span><br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">4</span><br>    <span class="hljs-keyword">if</span> data[<span class="hljs-number">0</span>][index + <span class="hljs-number">1</span>] != <span class="hljs-string">&#x27;白色&#x27;</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">4</span><br><br>    <span class="hljs-comment"># 绿色房子主人喝咖啡</span><br>    <span class="hljs-keyword">if</span> data[<span class="hljs-number">2</span>][index] != <span class="hljs-string">&#x27;咖啡&#x27;</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">5</span><br><br>    <span class="hljs-comment"># 抽Blends香烟的人住在养猫的人隔壁</span><br>    index = data[<span class="hljs-number">3</span>].index(<span class="hljs-string">&#x27;Blends&#x27;</span>)<br>    cat_index = data[<span class="hljs-number">4</span>].index(<span class="hljs-string">&#x27;猫&#x27;</span>)<br>    <span class="hljs-keyword">if</span> cat_index - index != <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> cat_index - index != -<span class="hljs-number">1</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">10</span><br><br>    <span class="hljs-comment"># 养马的人住抽Dunhill香烟的人隔壁</span><br>    index = data[<span class="hljs-number">3</span>].index(<span class="hljs-string">&#x27;Dunhill&#x27;</span>)<br>    horse_index = data[<span class="hljs-number">4</span>].index(<span class="hljs-string">&#x27;马&#x27;</span>)<br>    <span class="hljs-keyword">if</span> horse_index - index != <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> horse_index - index != -<span class="hljs-number">1</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">11</span><br><br>    <span class="hljs-comment"># 抽Blends香烟的人有一个喝水的邻居</span><br>    index = data[<span class="hljs-number">3</span>].index(<span class="hljs-string">&#x27;Blends&#x27;</span>)<br>    water_index = data[<span class="hljs-number">2</span>].index(<span class="hljs-string">&#x27;水&#x27;</span>)<br>    <span class="hljs-keyword">if</span> water_index - index != <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> water_index - index != -<span class="hljs-number">1</span>:<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">15</span><br><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;找到答案:&#x27;</span>)<br>    <span class="hljs-keyword">for</span> d_item <span class="hljs-keyword">in</span> data:<br>        <span class="hljs-built_in">print</span>(d_item)<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span><br><br><br><span class="hljs-comment"># **********************全排列列表******************************</span><br><br><br><span class="hljs-comment"># itertools.permutations 返回可迭代对象的所有数学全排列方式</span><br><span class="hljs-comment"># 以下是所有元素的全排列构成的列表</span><br>colour_list_all = <span class="hljs-built_in">list</span>(itertools.permutations([<span class="hljs-string">&#x27;红色&#x27;</span>, <span class="hljs-string">&#x27;黄色&#x27;</span>, <span class="hljs-string">&#x27;蓝色&#x27;</span>, <span class="hljs-string">&#x27;白色&#x27;</span>, <span class="hljs-string">&#x27;绿色&#x27;</span>], <span class="hljs-number">5</span>))<br>country_list_all = <span class="hljs-built_in">list</span>(itertools.permutations([<span class="hljs-string">&#x27;英国&#x27;</span>, <span class="hljs-string">&#x27;丹麦&#x27;</span>, <span class="hljs-string">&#x27;挪威&#x27;</span>, <span class="hljs-string">&#x27;德国&#x27;</span>, <span class="hljs-string">&#x27;瑞典&#x27;</span>], <span class="hljs-number">5</span>))<br>drinks_list_all = <span class="hljs-built_in">list</span>(itertools.permutations([<span class="hljs-string">&#x27;茶&#x27;</span>, <span class="hljs-string">&#x27;水&#x27;</span>, <span class="hljs-string">&#x27;咖啡&#x27;</span>, <span class="hljs-string">&#x27;啤酒&#x27;</span>, <span class="hljs-string">&#x27;牛奶&#x27;</span>], <span class="hljs-number">5</span>))<br>smoke_list_all = <span class="hljs-built_in">list</span>(itertools.permutations([<span class="hljs-string">&#x27;Pall Mall&#x27;</span>, <span class="hljs-string">&#x27;Dunhill&#x27;</span>, <span class="hljs-string">&#x27;BlueMaster&#x27;</span>, <span class="hljs-string">&#x27;Blends&#x27;</span>, <span class="hljs-string">&#x27;Prince&#x27;</span>], <span class="hljs-number">5</span>))<br>pet_list_all = <span class="hljs-built_in">list</span>(itertools.permutations([<span class="hljs-string">&#x27;猫&#x27;</span>, <span class="hljs-string">&#x27;马&#x27;</span>, <span class="hljs-string">&#x27;鱼&#x27;</span>, <span class="hljs-string">&#x27;鸟&#x27;</span>, <span class="hljs-string">&#x27;狗&#x27;</span>], <span class="hljs-number">5</span>))<br><br><span class="hljs-comment"># **********************过滤后的全排列列表******************************</span><br><br><br><span class="hljs-comment"># 全排列数量过大以下是过滤后出的列表</span><br>colour_list = []<br>country_list = []<br>drinks_list = []<br>smoke_list = []<br>pet_list = []<br><br><span class="hljs-keyword">for</span> colour <span class="hljs-keyword">in</span> colour_list_all:<br>    colour = <span class="hljs-built_in">list</span>(colour)<br>    <span class="hljs-keyword">if</span> colour[<span class="hljs-number">1</span>] == <span class="hljs-string">&#x27;蓝色&#x27;</span>:<br>        colour_list.append(colour)<br><br><span class="hljs-keyword">for</span> country <span class="hljs-keyword">in</span> country_list_all:<br>    country = <span class="hljs-built_in">list</span>(country)<br>    <span class="hljs-keyword">if</span> country[<span class="hljs-number">0</span>] == <span class="hljs-string">&#x27;挪威&#x27;</span>:<br>        country_list.append(country)<br><br><span class="hljs-keyword">for</span> drinks <span class="hljs-keyword">in</span> drinks_list_all:<br>    drinks = <span class="hljs-built_in">list</span>(drinks)<br>    <span class="hljs-keyword">if</span> drinks[<span class="hljs-number">2</span>] == <span class="hljs-string">&#x27;牛奶&#x27;</span>:<br>        drinks_list.append(drinks)<br><br><span class="hljs-comment"># 5! = 120</span><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(smoke_list_all)):<br>    smoke_list.append(<span class="hljs-built_in">list</span>(smoke_list_all[i]))<br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(pet_list_all)):<br>    pet_list.append(<span class="hljs-built_in">list</span>(pet_list_all[i]))<br><br><span class="hljs-comment"># **********************遍历所有可能的方案并进行匹配******************************</span><br><span class="hljs-comment"># 由于程序采用穷举法，效率较低，故记录时间。</span><br>start = time.time()<br><br><br><span class="hljs-keyword">for</span> country <span class="hljs-keyword">in</span> country_list:<br>    <span class="hljs-keyword">for</span> drinks <span class="hljs-keyword">in</span> drinks_list:<br>        <span class="hljs-keyword">for</span> smoke <span class="hljs-keyword">in</span> smoke_list:<br>            <span class="hljs-keyword">for</span> pet <span class="hljs-keyword">in</span> pet_list:<br>                <span class="hljs-keyword">for</span> colour <span class="hljs-keyword">in</span> colour_list:<br>                    data_list = [colour, country, drinks, smoke, pet]<br>                    <span class="hljs-comment"># ~ num += 1</span><br>                    <span class="hljs-keyword">if</span> handle(data_list) == <span class="hljs-number">0</span>:<br>                        <span class="hljs-comment"># ~ print(&quot;总运行次数 %d&quot; % num)</span><br>                        exit()<br>        end = time.time()<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\r计算用时:%0.4f秒&quot;</span> % (end - start), end=<span class="hljs-string">&quot;&quot;</span>)<br><br></code></pre></td></tr></table></figure>

<h1 id="其他解法"><a href="#其他解法" class="headerlink" title="其他解法"></a>其他解法</h1><p>利用google or-tools 求解<br><a target="_blank" rel="noopener" href="https://github.com/google/or-tools/blob/master/examples/python/zebra_sat.py">https://github.com/google/or-tools/blob/master/examples/python/zebra_sat.py</a></p>
<p>prolog求解<br><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_45841983/article/details/109813808">https://blog.csdn.net/weixin_45841983/article/details/109813808</a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/">人工智能</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Python/">Python</a>
                    
                      <a class="hover-with-bg" href="/tags/%E9%80%BB%E8%BE%91%E6%8E%A8%E7%90%86/">逻辑推理</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/10/02/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD__%E8%8A%B1%E5%8D%89%E8%AF%86%E5%88%AB(tensorflow)/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">花卉识别(tensorflow)</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/06/21/%E5%9F%BA%E4%BA%8EOpenCv%E7%9A%84%E4%BA%BA%E8%84%B8%E8%AF%86%E5%88%AB%EF%BC%88Python%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81%EF%BC%89/">
                        <span class="hidden-mobile">基于OpenCv的人脸识别</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://cheney822.gitee.io/" target="_blank" rel="nofollow noopener"><span>备用网址</span></a> 
  </div>
  

  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        皖ICP备2022002876号-1
      </a>
    </span>
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
